﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using nGSO.Model;
using Be.Timvw.Framework.ComponentModel;
using nGSO.Performance;

namespace nGSO.UI
{
    public partial class KPRaport : Form
    {
        public KPRaport()
        {
            InitializeComponent();

            using (DataModelEntities ctx = PerfDatabase.GetContext())
            {
                this.perfTestsBindingSource.DataSource
                    = ctx.PerfTests.Where(s => s.FunctionKey.StartsWith("KP Problem") && s.EndDate != null)
                        .OrderByDescending(s=>s.CreateDate).ToList();
            }    
            
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (grvKpTests.SelectedRows.Count == 1)
            {
                using (DataModelEntities ctx = PerfDatabase.GetContext())
                {
                    PerfTests seltest = grvKpTests.SelectedRows[0].DataBoundItem as PerfTests;
                    ctx.PerfTests.Attach(seltest);
                    PerfKpResult kpRes = seltest.PerfKpResult_1;
                    int maxCapacity = kpRes.MaxCapacity;
                    int desiredCost = kpRes.DesiredCost;

                    var bestRes = seltest.PerfKp.OrderByDescending(s => s.Effecinity).First();
                    kpRes.BestSolutionWithWeight = bestRes.Effecinity.ToString();

                    bindingKpDetails.DataSource = kpRes;

                    SortableBindingList<PerfKp> sortListWrapper = new SortableBindingList<PerfKp>(seltest.PerfKp.ToList());
                    grvKPTestDetails.DataSource = sortListWrapper;
                }
            }
            
        }
    }
}
